package com.mine.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mine.pojo.MineShopFans;
import com.mine.pojo.MineUser;
import com.mine.pojo.MineUserFeedback;
import com.mine.pojo.MineUserFeedbackCategory;
import com.mine.result.CodeMsg;
import com.mine.result.Result;
import com.mine.service.UserService;
import com.mine.vo.LoginVo;
import com.mine.vo.Register;

@Controller
public class UserController {

	@Autowired
	UserService userService;
	
	
	/**
	 * 用户登陆
	 * @param loginVo
	 * @return
	 */
	@RequestMapping("user/login")
    @ResponseBody
    public Result doLogin(LoginVo loginVo) {
		
    	if (loginVo==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	if (loginVo.getMobile()==null||loginVo.getMobile().equals("")) {
    		return Result.error(CodeMsg.MOBILE_EMPTY);
		}
    	if(loginVo.getPassword()==null||loginVo.getPassword().equals("")){
    		return Result.error(CodeMsg.PASSWORD_EMPTY);
		}

    	//System.out.println(loginVo);
    	//登录
    	Result reult = userService.login(loginVo);
    	return reult;
    }
	
	/**
	 * 用户注册 user/register
	 * @param loginVo
	 * @return
	 */
	@RequestMapping(value="user/register",method=RequestMethod.POST)
    @ResponseBody
    public Result register(@RequestBody Register vo) {
    	if (vo==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	if (vo.getMobile()==null||vo.getMobile().equals("")) {
    		return Result.error(CodeMsg.MOBILE_EMPTY);
		}
    	if(vo.getPassword()==null||vo.getPassword().equals("")){
    		return Result.error(CodeMsg.PASSWORD_EMPTY);
		}
    	if(vo.getVerifyCode()==null||vo.getVerifyCode().equals("")){
    		return Result.error(CodeMsg.VERIFYCODE_EMPTY);
		}

    	//System.out.println(vo);
    	//注册结果
    	Result result = userService.register(vo);
    	return result;
    }
	
	/**
	 * 用户修改密码
	 * @param loginVo
	 * @return
	 */
	@RequestMapping("user/modifyPassword")
    @ResponseBody
    public Result modifyPassword(@RequestParam("token")String token,@RequestParam("oldPassword")String oldPassword,@RequestParam("newPassword")String newPassword) {
    	if (token==null||token.equals("")) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	if (oldPassword==null||oldPassword.equals("")) {
    		return Result.error("旧密码不能为空");
		}
    	if(newPassword==null||newPassword.equals("")){
    		return Result.error("新密码不能为空");
		}
    	if(newPassword.equals(oldPassword)){
    		return Result.error("新旧密码不能相同");
		}
    	
    	Result result = userService.modifyPassword(token,oldPassword,newPassword);
    	
    	return result;
    }
	/**
	 * 更新用户信息
	 * @param user
	 * @return
	 */
	@RequestMapping(value="user/updateUserInfo/{token}",method=RequestMethod.POST)
    @ResponseBody
    public Result updateUserInfo(@PathVariable String token,@RequestBody MineUser user) {
    	if (user==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	user.setPhone(null);//不能任意修改手机号码
    	Result result = userService.updateUserInfo(token,user);
    	
    	return result;
    }
	
	/**
	 * 用户添加意见反馈
	 * @param token
	 * @param userFeedback
	 * @return
	 */
	@RequestMapping(value="user/feedback/add/{token}",method=RequestMethod.POST)
    @ResponseBody
    public Result addUserFeedback(@PathVariable String token,@RequestBody MineUserFeedback userFeedback) {
    	if (userFeedback==null||token==null||userFeedback.getCategoryId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	Result result = userService.addUserFeedback(token,userFeedback);
    	
    	return result;
    }
	
	/**
	 * 管理员添加意见反馈分类
	 * @param token
	 * @param feedbackCategory
	 * @return
	 */
	@RequestMapping(value="user/feedbackCategory/add/{token}",method=RequestMethod.POST)
    @ResponseBody
    public Result addUserFeedbackCategory(@PathVariable String token,@RequestBody MineUserFeedbackCategory feedbackCategory){
		if (feedbackCategory==null||token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	Result result = userService.addUserFeedbackCategory(token,feedbackCategory);
    	
    	return result;
    }

	/**
	 * 用户关注某一个店铺
	 * @param token
	 * @param fans
	 * @return
	 */
	@RequestMapping(value="user/followShop/add/{token}",method=RequestMethod.POST)
    @ResponseBody
    public Result addUserFollowShop(@PathVariable String token,@RequestBody MineShopFans fans){
		if (fans==null||token==null||fans.getShopId()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
    	Result result = userService.addUserFollowShop(token,fans);
    	
    	return result;
    }
	
	/**
	 * 根据用户token获取用户信息
	 * @param token
	 * @return
	 */
	@RequestMapping("user/getUserInfo/{token}")
    @ResponseBody
    public Result getUserInfo(@PathVariable String token) {
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		return userService.getUserByToken(token);
		
	}
	
	/**
	 * 获取用户消息数 订单及消息数目
	 * @param token
	 * @return
	 */
	@RequestMapping("user/getUserMsgCount/{token}")
    @ResponseBody
    public Result getUserMsgCount(@PathVariable String token) {
		if (token==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		return userService.getUserMsgCount(token);
		
	}
	
	/**
	 * 获取意见反馈类别
	 * @param token
	 * @return
	 */
	@RequestMapping("user/getUserFeedbackCate")
    @ResponseBody
    public Result getUserFeedbackCate() {
		
		return userService.getUserFeedbackCate();
		
	}
	
	/**
	 * 用户提交反馈
	 * @param token
	 * @return
	 */
	@RequestMapping(value="user/saveUserFeedback/{token}",method=RequestMethod.POST)
    @ResponseBody
    public Result saveUserFeedback(@PathVariable String token,@RequestBody MineUserFeedback userFeedback) {
		if (token==null||userFeedback==null||userFeedback.getCategoryId()==null||userFeedback.getContent()==null) {
			return Result.error(CodeMsg.BIND_ERROR);
		}
		return userService.saveUserFeedback(token, userFeedback);
		
	}
	
	@RequestMapping("getItemList")
    @ResponseBody
    public Result getItems(int page,int rows) {
		
		return userService.getItemList(page,rows);
		
	}
	
	
}
